cmake_minimum_required(VERSION 3.16)

project(pm VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(QT_QML_GENERATE_QMLLS_INI ON)
set(QML_IMPORT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/qml")
# if(NOT DEFINED INSTALL_EXAMPLESDIR)
#     set(INSTALL_EXAMPLESDIR "examples")
# endif()

# set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/demos/${PROJECT_NAME}")
# set(OUTPUT_DIRECTORY "${INSTALL_EXAMPLESDIR}/demos/${PROJECT_NAME}")

include(ExternalProject)

if(POLICY QTP0002)
    qt_policy(QTP0002 NEW)
endif()
message("CMAKE_SYSTEM_NAME ${CMAKE_SYSTEM_NAME}")
message("CMAKE_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR}")
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
   set(CMAKE_PREFIX_PATH C:/Qt/5.15.2/mingw81_64)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    message(STATUS "Platform: Linux")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    message(STATUS "Platform: macOS")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Android")
    if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
        set(CMAKE_PREFIX_PATH C:/Qt/6.8.3/android_x86_64)
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
         set(CMAKE_PREFIX_PATH C:/Qt/6.8.3/android_arm64_v8a)
    elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "armv7-a")
     set(CMAKE_PREFIX_PATH C:/Qt/6.8.3/android_armv7)
    endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
else()
    message(STATUS "Platform: Other: ${CMAKE_SYSTEM_NAME}")
endif()

message("CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH}")
message("CMAKE_TOOLCHAIN_FILE ${CMAKE_TOOLCHAIN_FILE}")
message("CMAKE_ANDROID_ARCH_ABI ${CMAKE_ANDROID_ARCH_ABI}")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(0)
# 配置 QMdnsEngine 外部项目
ExternalProject_Add(
    QMdnsEngine
    GIT_REPOSITORY https://github.com/nitroshare/qmdnsengine.git
    GIT_TAG 0.2.1
    PREFIX ${CMAKE_BINARY_DIR}/qmdnsengine
    CMAKE_ARGS
        -DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/qmdnsengine/install
        -DBUILD_EXAMPLES=OFF
        -DBUILD_DOC=OFF
        -DBUILD_TESTS=OFF
        -DCMAKE_PREFIX_PATH=${CMAKE_PREFIX_PATH}
        -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
        -DCMAKE_ANDROID_ARCH_ABI=${CMAKE_ANDROID_ARCH_ABI}
)
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Quick )
    find_package(Qt${QT_VERSION_MAJOR}  REQUIRED COMPONENTS Core Quick Network)
else()
    find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core Quick Positioning Sensors Network)
    find_package(Qt${QT_VERSION_MAJOR}  REQUIRED COMPONENTS Core Quick Positioning Sensors Network)

endif()
#find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)
file(GLOB_RECURSE SRC_FILES
        "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
        "${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp"
        "${CMAKE_CURRENT_SOURCE_DIR}/src/*.c"

)
set(PROJECT_SOURCES
        ${SRC_FILES}
       main.cpp
        qml.qrc
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    find_package(Qt6 REQUIRED COMPONENTS Core)
    find_package(Qt6 REQUIRED COMPONENTS Core)
    qt_add_executable(${PROJECT_NAME}
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
        android/AndroidManifest.xml
        android/build.gradle
        android/res/values/libs.xml
        android/res/xml/qtprovider_paths.xml
        android/gradle/wrapper/gradle-wrapper.jar
        android/gradle/wrapper/gradle-wrapper.properties
        android/gradle.properties android/gradlew
        android/gradlew.bat
        src/udp/udp.h src/udp/udp.cpp
    )
else()

if(ANDROID)

        add_library(${PROJECT_NAME} SHARED
            ${PROJECT_SOURCES}
        )
    else()
        find_package(Qt5 REQUIRED COMPONENTS Core)
find_package(Qt5 REQUIRED COMPONENTS Core)

        add_executable(${PROJECT_NAME}
          ${PROJECT_SOURCES}


        )
    endif()

endif()


if(ANDROID)
    set_property(TARGET ${PROJECT_NAME}
        APPEND
        PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
        ${CMAKE_CURRENT_SOURCE_DIR}/android)

endif()
target_link_directories(${PROJECT_NAME} PRIVATE
    ${CMAKE_BINARY_DIR}/qmdnsengine/install/lib
)
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
    target_link_libraries(${PROJECT_NAME}
      PRIVATE
      Qt${QT_VERSION_MAJOR}::Core
      Qt${QT_VERSION_MAJOR}::Quick
      Qt${QT_VERSION_MAJOR}::Network
    )
else()
target_link_libraries(${PROJECT_NAME}
  PRIVATE
  Qt${QT_VERSION_MAJOR}::Core
  Qt${QT_VERSION_MAJOR}::Quick
  Qt${QT_VERSION_MAJOR}::Network
  Qt${QT_VERSION_MAJOR}::Positioning
  Qt${QT_VERSION_MAJOR}::Sensors
)
endif()

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
  set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.xqyr.${PROJECT_NAME})
endif()
set_target_properties(${PROJECT_NAME} PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)


#add_dependencies(${PROJECT_NAME} QMdnsEngine)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(QT_VERSION_MAJOR EQUAL 6)
    qt_import_qml_plugins(${PROJECT_NAME})
    qt_finalize_executable(${PROJECT_NAME})
endif()

target_include_directories(${PROJECT_NAME}
    PRIVATE   # 仅用于此目标的编译
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_BINARY_DIR}/qmdnsengine/install/include
)



if (ANDROID)
    include(C:/Users/25137/AppData/Local/Android/Sdk/android_openssl/CMakeLists.txt)
endif()
